package com.rfsp.demo.controller;

import com.itextpdf.text.DocumentException;
import com.rfsp.common.base.BaseController;
import com.rfsp.common.util.PDFUtil;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;

@Controller
@RequestMapping("/demo")
public class DemoPdfController extends BaseController {
	
	@RequestMapping("/pdf")
	public ResponseEntity<byte[]> htmlToPdf(ModelMap model) throws DocumentException, IOException {
		model.put("aaa", "中国");
		model.put("bbb", "English；");

		return PDFUtil.toPdf("demo/pdf", model, "demo.pdf");
	}
	@RequestMapping("/pdf2")
	public ResponseEntity<byte[]> urlToPdf() throws DocumentException, IOException {

		String str = PDFUtil.toHtml("http://127.0.0.1:17018/mcp/demo/goods");
		return PDFUtil.toPdf(str, "goods.pdf");
	}

	@RequestMapping("/pdf3")
	public ResponseEntity<byte[]> pdfWater() throws DocumentException, IOException {
		String html = PDFUtil.toHtml("http://127.0.0.1:17018/mcp/demo/goods");
		byte[] base64 = PDFUtil.htmlToBase64(html);

		byte[] base64O = PDFUtil.buildPdf(base64
				, true, "水印测试123"
				, true, "页眉测试123"
				, true, ResourceUtils.getURL("classpath:static/images/logo.jpg").toString(), 510, 50, 40, -50
				, true, ResourceUtils.getURL("classpath:static/images/logo.png").toString()

		);

//		String pdfFullPath = PdfHtmlUtil.base64ToPDF(base64O, "rfpfile/aaa.pdf");
//		System.out.println(pdfFullPath);

//		byte[] fb = FileUtils.readFileToByteArray(new File(pdfFullPath));

		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_PDF);
		headers.setContentDisposition(ContentDisposition.attachment().filename("water.pdf").build());
		return ResponseEntity.ok().headers(headers).body(base64O);
	}


	@RequestMapping("/goods")
	public String path() throws DocumentException, IOException {
		return "demo/goods";
	}


}
